/**
 *
 */
package com.angel.data.generator.validators.impl;

import java.lang.reflect.Field;

import com.angel.architecture.flex.locator.ObjectLocator;
import com.angel.common.helpers.AnnotationHelper;
import com.angel.common.helpers.ReflectionHelper;
import com.angel.common.helpers.StringHelper;
import com.angel.data.generator.annotations.Inject;
import com.angel.data.generator.exceptions.DataGeneratorBuilderValidatorException;
import com.angel.data.generator.validators.DataGeneratorBuilderValidator;

/**
 * @author William
 *
 */
public class InjectFieldAnnotationValidator implements DataGeneratorBuilderValidator{

	public void validate(Class<?> dataGeneratorClass) throws DataGeneratorBuilderValidatorException {
		Field[] fields = ReflectionHelper.getAllFieldWith(Inject.class, dataGeneratorClass);
		for(Field field: fields){
			Inject inject = AnnotationHelper.getAnnotation(field, Inject.class);
			boolean checkInjection = inject.checkInjection();
			if(checkInjection){
				boolean containsInSpring = true;
				if(StringHelper.EMPTY_STRING.equals(inject.beanName())){
					containsInSpring = ObjectLocator.contains(field.getType());
				} else {
					containsInSpring = ObjectLocator.contains(inject.beanName());
				}
				if(!containsInSpring){
					throw new DataGeneratorBuilderValidatorException("Cannot find bean with [" + field.getType() + "] or [" + inject.beanName() +  "] in field [" + field.getName() + "] in Data object generator class [" + dataGeneratorClass + "] not contains Generator.class annotation.");
				}				
			}
		}
	}
}
